package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.fegin.clients.ItemClient;
import com.hmall.fegin.clients.UserClient;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderDto;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private UserClient userClient;
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    @Transactional
    public Long order(OrderDto orderDto) {
        Item item = itemClient.getItemById(orderDto.getItemId());
        Long totalFee = item.getPrice()* orderDto.getNum();
        Order order = new Order();
        order.setStatus(1);
        order.setTotalFee(totalFee);
        order.setPaymentType(orderDto.getPaymentType());
        ////////
        order.setUserId(2L);
        Date date = new Date();
        order.setCreateTime(date);
        orderMapper.insert(order);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setItemId(orderDto.getItemId());
        orderDetail.setNum(orderDto.getNum());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setName(item.getName());
        orderDetailMapper.insert(orderDetail);

        Address address = userClient.getById(orderDto.getAddressId());
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(order.getId());
        orderLogistics.setContact(address.getContact());
        orderLogistics.setMobile(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setCreateTime(new Date());
        orderLogisticsMapper.insert(orderLogistics);

        itemClient.updateStock(item.getId(), orderDto.getNum());


   // 1.创建消息
    String message = order.getId().toString();
    // 2.发送消息，利用消息后置处理器添加消息头
    rabbitTemplate.convertAndSend("delay.direct", "delay", message, new MessagePostProcessor() {
        @Override
        public Message postProcessMessage(Message message) throws AmqpException {
            // 添加延迟消息属性
            message.getMessageProperties().setDelay(1000);
            return message;
        }
    });
    return order.getId();
    }


    @Override
    public void updateStatus(Long aLong) {
        if (orderMapper.selectById(aLong).getStatus()==1){
            orderMapper.updateStutas(aLong);
        }
        return;
    }
}
